/**
 * Copyright 2008 floop.pl
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package org.eaicompiler.sqlgen.builders;

import java.util.Iterator;
import java.util.SortedMap;

import org.eaicompiler.model.Action;
import org.eaicompiler.model.Event;
import org.eaicompiler.model.ScriptDefinition;

/**
 * Builder for EventAI scripts SQL
 * 
 * @author floop.pl
 */
public class EventaiScriptsSQLBuilder extends SQLBuilder {
	private StringBuilder							sql;
	private boolean									sqlBuilt	= false;
	private boolean									useDelete;
	private SortedMap<Integer, ScriptDefinition>	creatures;

	public EventaiScriptsSQLBuilder(boolean useDelete, SortedMap<Integer, ScriptDefinition> creatures) {
		super();
		this.useDelete = useDelete;
		this.sql = new StringBuilder();
		this.creatures = creatures;
	}

	public String buildSQLForCreatures() {
		if (!sqlBuilt) {
			if (useDelete) {
				boolean firstDelete = true;
				boolean firstElement = true;
				int count = 0;
				Iterator<Integer> it = creatures.keySet().iterator();
				while (it.hasNext()) {
					Integer id = it.next();
					if (firstDelete) {
						sql.append("delete from `eventai_scripts` where `creature_id` in\n\t(");
						firstDelete = false;
					}
					if (!firstElement) {
						sql.append(",");
					} else {
						firstElement = false;
					}
					if (count % MAX_DELETE_IN_LINE == MAX_DELETE_IN_LINE - 1) {
						sql.append("\n\t");
					}
					sql.append("'");
					sql.append(id);
					sql.append("'");
					count++;
				}
				if (!firstElement) {
					sql.append(");\n");
				}
			}
			boolean firstInsert = true;
			boolean firstElement = true;
			Iterator<Integer> it = creatures.keySet().iterator();
			while (it.hasNext()) {
				Integer id = it.next();
				Iterator<Event> i = creatures.get(id).getEvents().iterator();
				int event_id = id.intValue()*100;
				while (i.hasNext()) {
					Event e = i.next();
					if (firstInsert) {
						sql.append("insert into `eventai_scripts`(`id`,`creature_id`,`event_type`,`event_inverse_phase_mask`,`event_chance`,`event_flags`,\n");
						sql.append("\t`event_param1`,`event_param2`,`event_param3`,`event_param4`,\n");
						sql.append("\t`action1_type`,`action1_param1`,`action1_param2`,`action1_param3`,\n");
						sql.append("\t`action2_type`,`action2_param1`,`action2_param2`,`action2_param3`,\n");
						sql.append("\t`action3_type`,`action3_param1`,`action3_param2`,`action3_param3`,\n");
						sql.append("\t`comment`) values\n");
						firstInsert = false;
					}
					if (!firstElement) {
						sql.append(",\n");
					} else {
						firstElement = false;
					}					
					sql.append("\t('");
					sql.append(event_id++);
					sql.append("','");
					sql.append(id.intValue());
					sql.append("','");
					sql.append(e.getType());
					sql.append("','");
					sql.append(e.getInversePhaseMask());
					sql.append("','");
					sql.append(e.getChance());
					sql.append("','");
					sql.append(e.getEventFlags());
					sql.append("',");
					for (int f = 0; f < Event.MAX_EVENT_PARAMS; f++) {
						sql.append("'");
						sql.append(e.getParam(f));
						sql.append("'");
						if (f < Event.MAX_EVENT_PARAMS - 1) {
							sql.append(",");
						}
					}
					sql.append(",");
					for (int f = 0; f < Event.MAX_EVENT_ACTIONS; f++) {
						Action a = e.getAction(f);
						if (a == null) {
							// Make sure we have any action
							a = new Action();
						}
						a = a.getActionForCreature(id.intValue());
						sql.append("'");
						sql.append(a.getType());
						sql.append("',");
						for (int z = 0; z < Action.MAX_ACTION_PARAMS; z++) {
							sql.append("'");
							sql.append(a.getParam(z));
							sql.append("'");
							if (z < Action.MAX_ACTION_PARAMS - 1) {
								sql.append(",");
							}
						}
						if (f < Event.MAX_EVENT_ACTIONS - 1) {
							sql.append(",");

						}
					}
					sql.append(",'");
					if (e.getComment() != null) {
						sql.append(escapeStringForSql(e.getComment()));
					}
					sql.append("')");
				}
			}
			if(!firstElement)
			{
				sql.append(";\n");
			}			
			sqlBuilt = true;			
		}
		return sql.toString();
	}
}
